﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateAdminPage.Classes.FrmClasses
{
    #region USING
    using GenerateAdminPage.Classes.Helpers;
    using GenerateAdminPage.Classes.DBStructure;
    #endregion

    public class FrmClassOneToMany : AbstractFrmClass
    {
        public override string GenerateClass()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB + "public partial class FrmCapNhat" + TblMany.Name + " : Office2007Form");
            Result.AppendLine(TAB + "{");

            Result.AppendLine(GenerateVariables());
            Result.AppendLine(GenerateConstructor());
            Result.AppendLine(GenerateInitHiddenFields());
            Result.AppendLine(GeneratebtnChonThamChieu_Click());
            Result.AppendLine(GenerateValidateInput());
            Result.AppendLine(GeneratebtnCapNhat_Click());
            Result.AppendLine(GenerateTransferDataInfo());
            Result.AppendLine(GeneratebtnThoat_Click());
            Result.AppendLine(GenerateKeyPress());

            Result.AppendLine(TAB + "}");

            return Result.ToString();
        }

        public string GenerateVariables()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public EventHandler Handler { get; set; }");

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (TblMany.Attributes[i].IsForeignKey)
                {
                    if (TblMany.Attributes[i].ReferTo != TblOne.Name)
                    {
                        Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.TextBoxX txt" + TblMany.Attributes[i].Name + ";");
                    }
                }
            }

            return Result.ToString();
        }

        public string GenerateConstructor()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public FrmCapNhat" + TblMany.Name + "()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "InitializeComponent();");
            Result.AppendLine(TAB3 + "InitHiddenFields();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateInitHiddenFields()
        {
            StringBuilder Result = new StringBuilder();
            var found = false;

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (TblMany.Attributes[i].IsForeignKey)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                return Result.ToString();
            }

            Result.AppendLine(TAB2 + "public void InitHiddenFields()");
            Result.AppendLine(TAB2 + "{");

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (TblMany.Attributes[i].IsForeignKey)
                {
                    if (TblMany.Attributes[i].ReferTo != TblOne.Name)
                    {
                        Result.AppendLine(TAB3 + "txt" + TblMany.Attributes[i].Name + " = new DevComponents.DotNetBar.Controls.TextBoxX");
                        Result.AppendLine(TAB3 + "{");
                        Result.AppendLine(TAB4 + "Name = \"txt" + TblMany.Attributes[i].Name + "\",");
                        Result.AppendLine(TAB4 + "Text = \"\"");
                        Result.AppendLine(TAB3 + "};");
                        Result.AppendLine(TAB3 + "txt" + TblMany.Attributes[i].Name + ".Visible = false;");
                    }
                }
            }   

            Result.AppendLine(TAB2 + "}");
            return Result.ToString();
        }

        public string GeneratebtnChonThamChieu_Click()
        {
            StringBuilder Result = new StringBuilder();

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (TblMany.Attributes[i].IsForeignKey)
                {
                    if (TblMany.Attributes[i].ReferTo != TblOne.Name)
                    {
                        Result.AppendLine(TAB2 + "private void btnChon" + TblMany.Attributes[i].ReferTo + "_Click(object sender, EventArgs e)");
                        Result.AppendLine(TAB2 + "{");
                        Result.AppendLine(TAB3 + "FrmQuanLy" + TblMany.Attributes[i].ReferTo + " frm = new FrmQuanLy" + TblMany.Attributes[i].ReferTo + "(true);");
                        Result.AppendLine(TAB3 + "frm.Handler += Get" + TblMany.Attributes[i].ReferTo + ";");
                        Result.AppendLine(TAB3 + "frm.ShowDialog();");
                        Result.AppendLine(TAB2 + "}" + END);
                        Result.AppendLine(GenerateGetThamChieu(Utils.GetTableByName(DB, TblMany.Attributes[i].ReferTo)));
                    }
                }
            }

            return Result.ToString();
        }

        public string GenerateGetThamChieu(Table _tbl)
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public void Get" + _tbl.Name + "(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var eventType = (TransferSimpleDataEvent)e;");
            Result.AppendLine(TAB3 + "string[] comp = eventType.Data.Split(new char[] { '#' });");
            Result.AppendLine(TAB3 + "txt" + Utils.GetPK(_tbl).Name + ".Text = comp[0];");
            Result.AppendLine(TAB3 + "txt" + _tbl.Name + ".Text = comp[1];");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateValidateInput()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private bool ValidateInput(ref string errorText)");
            Result.AppendLine(TAB2 + "{");

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (TblMany.Attributes[i].IsForeignKey)
                {
                    if (TblMany.Attributes[i].ReferTo != TblOne.Name)
                    {
                        if (TblMany.Attributes[i].Type == DataType.DATETIME)
                        {
                            Result.AppendLine(TAB3 + "if (dt" + TblMany.Attributes[i].Name + ".Value == DateTime.MinValue)");
                            Result.AppendLine(TAB3 + "{");
                            Result.AppendLine(TAB4 + " errorText = \"Vui lòng chọn " + TblMany.Attributes[i].Name + "\";");
                            Result.AppendLine(TAB4 + "return false;");
                            Result.AppendLine(TAB3 + "}");
                        }
                        else if (TblMany.Attributes[i].Type == DataType.BOOL)
                        {
                            Result.AppendLine(TAB3 + "if (cbx" + TblMany.Attributes[i].Name + ".SelectedIndex == -1)");
                            Result.AppendLine(TAB3 + "{");
                            Result.AppendLine(TAB4 + " errorText = \"Vui lòng chọn " + TblMany.Attributes[i].Name + "\";");
                            Result.AppendLine(TAB4 + "return false;");
                            Result.AppendLine(TAB3 + "}");
                        }
                        else if (TblMany.Attributes[i].Type == DataType.IMAGE)
                        {
                            Result.AppendLine(TAB3 + "if (ptb" + TblMany.Attributes[i].Name + ".Image == null)");
                            Result.AppendLine(TAB3 + "{");
                            Result.AppendLine(TAB4 + " errorText = \"Vui lòng chọn " + TblMany.Attributes[i].Name + "\";");
                            Result.AppendLine(TAB4 + "return false;");
                            Result.AppendLine(TAB3 + "}");
                        }
                        else
                        {
                            Result.AppendLine(TAB3 + "if (txt" + TblMany.Attributes[i].Name + ".Text == \"\")");
                            Result.AppendLine(TAB3 + "{");
                            Result.AppendLine(TAB4 + " errorText = \"Vui lòng nhập " + TblMany.Attributes[i].Name + "\";");
                            Result.AppendLine(TAB4 + "return false;");
                            Result.AppendLine(TAB3 + "}");
                        }
                    }
                }
            }

            Result.AppendLine(TAB3 + "return true;");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnCapNhat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnCapNhat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var errorText = \"\";");
            Result.AppendLine(TAB3 + "if (!ValidateInput(ref errorText))");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBox.Show(errorText, \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB4 + "return;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "TransferDataInfo(this, new TransferSimpleDataEvent(" + BuildListColumnData(TblMany) + "));");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateTransferDataInfo()
        {
            StringBuilder Result = new StringBuilder();

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "public void TransferDataInfo(object sender, TransferSimpleDataEvent e)");
                Result.AppendLine(TAB2 + "{");
                Result.AppendLine(TAB3 + "this.Close();");
                Result.AppendLine(TAB3 + "this.Handler(this, e);");
                Result.AppendLine(TAB2 + "}");
            }
            return Result.ToString();
        }

        public string BuildListColumnData(Table tbl)
        {
            StringBuilder Result = new StringBuilder();
            List<Attribute> lst = new List<Attribute>();

            for (int i = 0; i < tbl.Attributes.Count; i++)
            {
                if (tbl.Attributes[i].IsPrimaryKey ||
                    (tbl.Attributes[i].IsForeignKey && tbl.Attributes[i].Name == Utils.GetPK(TblOne).Name))
                {
                    continue;
                }
                lst.Add(tbl.Attributes[i]);
            }

            for (int i = 0; i < lst.Count; i++)
            {
                if (i < lst.Count - 1)
                {
                    if (lst[i].Type == DataType.DATETIME)
                    {
                        Result.Append("String.Format(\"{0:dd/MM/yyyy}\", dt" + lst[i].Name + ".Value) + \"#\" + ");
                    }
                    else if (lst[i].Type == DataType.BOOL)
                    {
                        Result.Append("cbx" + lst[i].Name + ".SelectedItems(0).Value + \"#\" + ");
                    }
                    else
                    {
                        Result.Append("txt" + lst[i].Name + ".Text + \"#\" + ");
                    }
                }
                else
                {
                    if (lst[i].Type == DataType.DATETIME)
                    {
                        Result.Append("String.Format(\"{0:dd/MM/yyyy}\", dt" + lst[i].Name + ".Value)");
                    }
                    else if (lst[i].Type == DataType.BOOL)
                    {
                        Result.Append("cbx" + lst[i].Name + ".SelectedItems(0).Value");
                    }
                    else
                    {
                        Result.Append("txt" + lst[i].Name + ".Text");
                    }
                }
            }
            return Result.ToString();
        }

        public string GeneratebtnThoat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnThoat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "this.Close();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateKeyPress()
        {
            StringBuilder Result = new StringBuilder();

            for (int i = 0; i < TblMany.Attributes.Count; i++)
            {
                if (!TblMany.Attributes[i].IsIdentify && !TblMany.Attributes[i].IsForeignKey)
                {
                    if (TblMany.Attributes[i].Type == DataType.INT ||
                        TblMany.Attributes[i].Type == DataType.DOUBLE ||
                        TblMany.Attributes[i].Type == DataType.FLOAT ||
                        TblMany.Attributes[i].Type == DataType.LONG)
                    {
                        Result.AppendLine(TAB2 + "private void txt" + TblMany.Attributes[i].Name + "_KeyPress(object sender, KeyPressEventArgs e)");
                        Result.AppendLine(TAB2 + "{");
                        Result.AppendLine(TAB3 + "if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')");
                        Result.AppendLine(TAB3 + "{");
                        Result.AppendLine(TAB4 + "e.Handled = true;");
                        Result.AppendLine(TAB3 + "}");
                        Result.AppendLine(TAB2 + "}");
                    }
                }
            }

            return Result.ToString();
        }
    }
}
